<script setup>
import { introApi } from "@/services/index";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { nextTick, ref, watch, onMounted } from "vue";
import { appInfo, loginTerminal } from "@/services/user";
const userInfo = ref({});
const isSwitch = ref("");
const topHeight = ref(uni.getStorageSync("topHight") || 0);
onLoad((e) => {
  code.value = e.code;
  isSwitch.value = e.isSwitch ? e.isSwitch : "";
  if (!isSwitch.value) {
    getList();
  }
  //
  userInfo.value = uni.getStorageSync("user")
    ? JSON.parse(uni.getStorageSync("user"))
    : {};
  if (userInfo.value.adminPermission && userInfo.value.storePermission) {
  } else {
    uni.reLaunch({ url: "/pages/index/index" });
  }
});

const code = ref("");
const introData = ref([]);

const getList = async () => {
  try {
    // 发送请求
    let res = await appInfo({});
    if (res.code == 200) {
      introData.value = res.data.employees;
    }
  } catch (e) {
    uni.showToast({
      title: e.info,
      icon: "none",
    });
  }
};
const selectFun = async (item) => {
  console.log(item);

  try {
    let res = await loginTerminal({
      terminal: item == 1 ? "shop" : "manager",
    });

    if (res.code == 200) {
      if (res.data.employees.length >= 1) {
        uni.setStorageSync("more", 1);
      } else {
        uni.setStorageSync("more", 0);
      }
      uni.setStorageSync("shopType", item);
      uni.setStorageSync("X-Auth-Token", res.data.token);
      uni.setStorageSync("user", JSON.stringify(res.data.user));
      uni.setStorageSync("shopType", item);
      // uni.setStorageSync("selectShopType", "admin");

      // uni.reLaunch({ url: "/pages/index/index" });
      if (item == 1) {
        uni.setStorageSync("selectShopType", "shop");
        setTimeout(() => {
          uni.reLaunch({ url: "/pages/index/index" });
        }, 1);
        // if (res.data.employees && res.data.employees.length > 1) {
        //   setTimeout(() => {
        //     uni.reLaunch({ url: "/pages/users/switch/index" });
        //   }, 1);
        // } else {
        //   setTimeout(() => {
        //     uni.reLaunch({ url: "/pages/index/index" });
        //   }, 1);
        // }
      } else {
        // uni.setStorageSync("shopType", item);
        uni.setStorageSync("selectShopType", "admin");

        uni.reLaunch({ url: "/pages/index/index" });
      }
    }

    // 发送请求
  } catch (e) {
    uni.showToast({
      title: e.info,
      icon: "none",
    });
  }
};

const onClickLeft = () => history.back();
const onClickRight = () => showToast("按钮");

const themeVars = ref({
  navBarIconColor: "#000000",
  navBarTextColor: "#000000",
  navBarBackground: "transparent",
});
</script>

<template>
  <!-- <van-config-provider :theme-vars="themeVars">
    <van-nav-bar
      title=" "
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
      :safe-area-inset-top="true"
      :fixed="true"
      z-index="10"
      :placeholder="true"
    />
  </van-config-provider> -->
  <top-nav :title="' '" :border="false" rType="save"></top-nav>
  <view class="content" :style="'top: calc( 88rpx + ' + topHeight + 'px);'">
    <view class="content-title">请选择</view>
    <view class="content-des">登录后可切换 </view>
  </view>
  <view class="content-list">
    <view class="content-list-item" @click="selectFun(2)">
      <view class="content-list-left">
        <view class="content-list-left-item">
          <view class="content-list-left-item-icon">
            <image src="@/static/home/shop-user.png" mode="scaleToFill" />
          </view>
          <view class="content-list-left-item-text">
            <view class="content-list-item-shop">{{ "运营端" }}</view>
            <view class="content-list-item-employeeNo"
              >{{ "登录管理平台，查看营业数据、报表" }}
            </view>
          </view>
        </view>
      </view>
      <view class="content-list-right">
        <van-icon name="arrow" size="28rpx" color="#000000" />
      </view>
    </view>
    <view class="content-list-item" @click="selectFun(1)">
      <view class="content-list-left">
        <view class="content-list-left-item">
          <view class="content-list-left-item-icon">
            <image src="@/static/home/shop-icon.png" mode="scaleToFill" />
          </view>
          <view class="content-list-left-item-text">
            <view class="content-list-item-shop">{{ "店铺端" }}</view>
            <view class="content-list-item-employeeNo"
              >{{ "登录操作平台，操作收银、盘存等" }}
            </view>
          </view>
        </view>
      </view>
      <view class="content-list-right">
        <van-icon name="arrow" size="28rpx" color="#000000" />
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
